﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace TileEditorApplication
{
    public partial class EnemyImport : Form
    {
        public Bitmap preview = null;

        public string fileName;
        public string objectName;

        public string enemiesFilePath;

        public int objectWidth;
        public int objectHeight;

        public EnemyImport()
        {
            InitializeComponent();

            CheckFilePath();

            while (LoadEnemies() == false)
            {
                CheckFilePath();
            }

            if (EnemyTypeComboBox.Items.Count > 0)
            {
                EnemyTypeComboBox.SelectedIndex = 0;
            }
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            if (preview != null)
            {
                Rectangle source = new Rectangle(0, 0, preview.Width, preview.Height);

                Rectangle dest = new Rectangle(0, 0, (int)(preview.Width * (ObjImportSplit.Panel1.Height / (float)preview.Height)), (int)(preview.Height * (ObjImportSplit.Panel1.Height / (float)preview.Height)));

                e.Graphics.DrawImage(preview, dest, source, GraphicsUnit.Pixel);
            }
        }

        private void ObjectImportButton_Click(object sender, EventArgs e)
        {            
            if (preview != null)
            {
                objectWidth = (int)ObjectWidthUpDown.Value;
                objectHeight = (int)ObjectHeightUpDown.Value;

                objectName = EnemyTypeComboBox.Items[EnemyTypeComboBox.SelectedIndex].ToString();

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void ObjectBrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "png|*.png|jpeg|*.jpg|bmp|*.bmp";
            dlg.FilterIndex = 1;
            dlg.FileName = enemiesFilePath;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                if (dlg.FileName != enemiesFilePath + "Graphics\\Enemies\\" + dlg.SafeFileName)
                {
                    MessageBox.Show(Form1.ActiveForm, "Unable to load image. Please load images from\nthe proper resource folder:\n" + enemiesFilePath + "\nGraphics\\Enemies\\");
                    return;
                }

                fileName = "Graphics\\Enemies\\" + dlg.SafeFileName;
                preview = new Bitmap(dlg.FileName);

                ObjectWidthUpDown.Value = preview.Width;
                ObjectHeightUpDown.Value = preview.Height;
                ObjImportSplit.Panel1.Invalidate();
                ObjectImportButton.Enabled = true;
            }
        }

        private bool LoadEnemies()
        {
            try
            {
                XElement root = XElement.Load(enemiesFilePath + "Data\\enemy_types.xml");

                IEnumerable<XElement> types = root.Elements();

                foreach (XElement t in types)
                {
                    string s = Convert.ToString(t.Value);
                    EnemyTypeComboBox.Items.Add(s);
                }
                return true;
            }
            catch
            {
                MessageBox.Show(Form1.ActiveForm, "Unable to load enemy types. Please load enemies from\nthe proper resource folder:\n" + enemiesFilePath);
                return false;
            }
        }

        public void CheckFilePath()
        {
            try
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(Application.StartupPath + "\\data\\resource_path.txt");

                enemiesFilePath = reader.ReadLine();

                reader.Close();
            }
            catch
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                dlg.Description = "Find the resources folder";

                if (DialogResult.OK == dlg.ShowDialog(this))
                {
                    enemiesFilePath = dlg.SelectedPath + "\\";
                }
            }
        }

        private void EnemyImport_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.IO.StreamWriter writer = new System.IO.StreamWriter(Application.StartupPath + "\\data\\resource_path.txt");
            writer.WriteLine(enemiesFilePath);

            writer.Close();
        }
    }
}
